package com.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.base.BaseDaoImpl;
import com.dao.UserVoteDao;
import com.entity.UserVote;
import com.util.OptionRatio;
import com.util.Page;
@Repository
public class UserVoteDaoImpl extends BaseDaoImpl<UserVote> implements UserVoteDao {

	@Override
	public Page getUvByUserId(int userid,Page p) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("vuid", userid);
		String hql = "from UserVote where voteUser.vuid = :vuid";
		return super.getPageByHql(hql, map, p);
	}

	@Override
	public List<OptionRatio> getOptionRatio(int vid) {
		Query query = super.sf.getCurrentSession().createQuery("select uv.voteOption.voText,count(uv.voteOption.voId) from UserVote uv where uv.vote.vid = ? group by uv.voteOption.voText");
		query.setParameter(0, vid);
		List<Object[]> list = query.list();
		List<OptionRatio> ors = new ArrayList<OptionRatio>();
		for(Object[] obj : list){
			ors.add(new OptionRatio(obj[0].toString(),Integer.parseInt(obj[1].toString())));
		}
		return ors;
	}

}
